const {
  findNewsListService,
  findNewsCategoryListService,
  findNewsByCategoryIdService,
  findNewsByIdService,
} = require("../../service/news");
const { ErrorRes, SuccessRes } = require("../../res-model/index");
const { findNewsListFailInfo } = require("../../res-model/failInfo/news");
/**
 * 查询所有新闻
 */
async function findNewsList() {
  let sites;
  try {
    sites = await findNewsListService();
  } catch (ex) {
    console.error("获取新闻列表失败", ex);
    return new ErrorRes(findNewsListFailInfo); // 数据库错误
  }
  // 查询失败
  if (sites == null)
    return new ErrorRes(findNewsListFailInfo, "获取新闻列表失败");

  // 查询成功
  return new SuccessRes(sites);
}
/**
 * 根据分类Id查找新闻
 * @returns
 */
async function findNewsByCategoryId(id) {
  let news = await findNewsByCategoryIdService(id);
  // 查询成功
  return new SuccessRes(news);
}
/**
 * 根据新闻id查找详情
 * @param {*} id
 * @returns
 */
async function findNewsById(id) {
  let news = await findNewsByIdService(id);
  // 查询成功
  return new SuccessRes(news);
}
/**
 * 查询所有新闻分类
 */
async function findNewsCategoryList() {
  let sites;
  try {
    sites = await findNewsCategoryListService();
  } catch (ex) {
    console.error("获取新闻分类列表失败", ex);
    return new ErrorRes(findNewsListFailInfo); // 数据库错误
  }
  // 查询失败
  if (sites == null)
    return new ErrorRes(findNewsListFailInfo, "获取新闻分类列表失败");

  // 查询成功
  return new SuccessRes(sites);
}

module.exports = {
  findNewsList,
  findNewsCategoryList,
  findNewsByCategoryId,
  findNewsById,
};
